package com.soso.gae.spider.service.impl;

import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.caucho.hessian.service.HessianService;
import com.soso.core.exception.GeneralExceptionHandler;
import com.soso.core.util.log.Logger;
import com.soso.core.util.other.JsonUtil;
import com.soso.crawl.hessian.api.ReadCrawlInfoApi;
import com.soso.crawl.hessian.view.CrawlInfoCacheBo;
import com.soso.gae.spider.model.CrawlInfoCacheTable;
import com.soso.gae.spider.service.BaseService;
import com.soso.spider.context.SpiderContext;
import com.soso.spider.memcache.item.CrawlInfoCacheItem;
import com.soso.spider.service.CrawlInfoService;

public class CrawlInfoServiceImpl implements CrawlInfoService{
	@Override
	public CrawlInfoCacheBo read(String readInfoDigest, String readInfoUrl) {
		CrawlInfoCacheItem cacheItem = (CrawlInfoCacheItem) SpiderContext.getInstance().getCommonCache().get(readInfoDigest, new CrawlInfoCacheItem());
		if (cacheItem.getValue() != null) {
			return (CrawlInfoCacheBo) cacheItem.getValue();
		}
		CrawlInfoCacheBo crawlCacheBo = null;
		synchronized (this) {
			String GQL = "select from " + CrawlInfoCacheTable.class.getName() + " where readInfoDigest=='" + readInfoDigest + "'";
			BaseService baseService = (BaseService)SpiderContext.getInstance().getBaseService();
			PersistenceManager pm=baseService.getPm();
			try{
				List<?> list = baseService.query(pm,GQL,0,1);
				if (list != null && !list.isEmpty()) {
					CrawlInfoCacheTable crawlCacheTable = (CrawlInfoCacheTable) list.get(0);
					crawlCacheBo = new CrawlInfoCacheBo();
					crawlCacheBo.setCrawlDataForm(crawlCacheTable.getCrawlDataForm());
					crawlCacheBo.setHessianCallback(crawlCacheTable.getCallback());
				}
				if (crawlCacheBo == null) {
					Logger.info("url:"+readInfoUrl+",md5:"+readInfoDigest);
					ReadCrawlInfoApi readApi = HessianService.call(ReadCrawlInfoApi.class, readInfoUrl);
					String jsonCrawlCacheBo = readApi.read(readInfoDigest);
					if (jsonCrawlCacheBo != null && !"".equals(jsonCrawlCacheBo)) {
						JsonUtil<CrawlInfoCacheBo> jsonUtil=new JsonUtil<CrawlInfoCacheBo>(){};
						try{
							crawlCacheBo=jsonUtil.fromJsonToObject(jsonCrawlCacheBo);
						}catch(Exception e){
							Logger.error(e.getMessage());
						}
					}
					if (crawlCacheBo != null) {
						CrawlInfoCacheTable crawlDataTable = new CrawlInfoCacheTable();
						crawlDataTable.setCallback(crawlCacheBo.getHessianCallback());
						crawlDataTable.setCrawlDataForm(crawlCacheBo.getCrawlDataForm());
						crawlDataTable.setCreateDate(new Date());
						crawlDataTable.setReadInfoDigest(readInfoDigest);
						baseService.create(pm,crawlDataTable);
					}
				}
				cacheItem.setValue(crawlCacheBo);
				SpiderContext.getInstance().getCommonCache().put(readInfoDigest, cacheItem);
			} catch (MalformedURLException e) {
				GeneralExceptionHandler.handle(e);
			} finally{
				pm.close();
			}
		}
		return crawlCacheBo;
	}

}
